package com.vanzo.talkie;

import android.util.Log;
import android.util.Xml;

import com.vanzo.bean.Channel;
import com.vanzo.util.LogUtil;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlSerializer;

import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by tisen on 2017/12/28.
 */

public class ChannelParser implements Parse {
    private static final String CHANNELS = "channels";
    private static final String CHANNEL = "channel";
    private static final String TITLE = "title";
    private static final String TFV = "tfv";
    private static final String RFV = "rfv";
    private static final String TX = "tx";
    private static final String RX = "rx";
    private static final String SQ = "sq";


    @Override
    public List<Channel> parse(XmlPullParser parser) throws Exception {
        List<Channel> channels = null;
        Channel channel = null;
        int eventType = parser.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT) {
            switch (eventType) {
                case XmlPullParser.START_DOCUMENT:
                    channels = new ArrayList<>();
                    break;
                case XmlPullParser.START_TAG:
                    LogUtil.put(this, "START_TAG" + "name = " + parser.getName(), LogUtil.WARN);
                    switch (parser.getName()) {
                        case CHANNEL:
                            channel = new Channel();
                            break;
                        case TITLE:
                            channel.setTitle(parser.nextText());
                            break;
                        case TFV:
                            channel.setTfv(parser.nextText());
                            break;
                        case RFV:
                            channel.setRfv(parser.nextText());
                            break;
                        case TX:
                            channel.setTx(parser.nextText());
                            break;
                        case RX:
                            channel.setRx(parser.nextText());
                            break;
                        case SQ:
                            channel.setSq(parser.nextText());
                            break;
                    }
                    break;
                case XmlPullParser.END_TAG:
                    if (parser.getName().equals(CHANNEL)) {
                        channels.add(channel);
                        channel = null;
                    }
                    break;
            }
            eventType = parser.next();
        }
        return channels;
    }

    @Override
    public String serialize(List<Channel> connects) throws Exception {
        XmlSerializer serializer = Xml.newSerializer();
        StringWriter writer = new StringWriter();
        serializer.setOutput(writer);
        serializer.startDocument("UTF-8", false);
        serializer.startTag("", CHANNELS);
        for (Channel connect : connects) {
            serializer.startTag("", CHANNEL);

            serializer.startTag("", TITLE);
            serializer.text(connect.getTitle() + "");
            serializer.endTag("", TITLE);

            serializer.startTag("", TFV);
            serializer.text(connect.getTfv() + "");
            serializer.endTag("", TFV);

            serializer.startTag("", RFV);
            serializer.text(connect.getRfv() + "");
            serializer.endTag("", RFV);

            serializer.startTag("", TX);
            serializer.text(connect.getTx() + "");
            serializer.endTag("", TX);

            serializer.startTag("", RX);
            serializer.text(connect.getRx() + "");
            serializer.endTag("", RX);

            serializer.startTag("", SQ);
            serializer.text(connect.getSq() + "");
            serializer.endTag("", SQ);

            serializer.endTag("", CHANNEL);
        }
        serializer.endTag("", CHANNELS);
        serializer.endDocument();
        return writer.toString();
    }

}
